package br.com.Opet.PIM.Utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Reflection {

	private static final SimpleDateFormat format = new SimpleDateFormat(
			"dd/MM/yyyy");

	public static String toString(Object obj) {
		if (obj == null)
			return "";
		String s = obj.getClass().getName();
		s = s.substring(s.lastIndexOf(".") + 1);
		StringBuffer stringbuffer = (new StringBuffer("\n\n============= [ "))
				.append(s).append(" - INICIO ] =============\n");
		if (obj instanceof Collections) {
			for (Iterator iterator = ((List) obj).iterator(); iterator
					.hasNext(); toString(iterator.next()))
				;
		} else {
			List list = null;
			try {
				list = getFields(obj.getClass());
			} catch (Exception exception) {
			}
			try {
				if (list == null || list.isEmpty()) {
					if ("java.util.Date".equalsIgnoreCase(s))
						stringbuffer.append("\n \t=> ").append(
								format.format(obj));
					else if ("java.util.List".equalsIgnoreCase(s)
							|| "java.util.ArrayList".equalsIgnoreCase(s)) {
						for (int i = 0; i < ((List) obj).size(); i++)
							toString(((List) obj).get(i));

					} else {
						stringbuffer.append("\n \t=> ").append(obj);
					}
				} else if (s.indexOf("java.lang") > -1) {
					stringbuffer.append("\n \t=> ").append(
							obj != null ? obj : "");
				} else {
					Collections.sort(list, new Comparator() {

						public int compare(Object obj3, Object obj4) {
							return ((Field) obj3).getName().compareTo(
									((Field) obj4).getName());
						}

					});
					Object obj1 = null;
					for (int j = 0; j < list.size(); j++) {
						Field field = (Field) list.get(j);
						String s1 = field.getName();
						Object obj2 = null;
						if (field.getModifiers() != 2
								&& field.getModifiers() != 4)
							continue;
						try {
							obj2 = getValue(obj, s1, new Class[0],
									new Object[0]);
						} catch (Exception exception2) {
							continue;
						}
						if ("java.util.Date".equalsIgnoreCase(field.getType()
								.getName())) {
							stringbuffer.append("\n \t=> ").append(s1)
									.append(" = ");
							stringbuffer.append(obj2 != null ? format
									.format(obj2) : "");
						} else {
							stringbuffer.append("\n \t=> ").append(s1)
									.append(" = ")
									.append(obj2 != null ? obj2 : "");
						}
					}

				}
			} catch (Exception exception1) {
				stringbuffer = new StringBuffer(
						"\n\nErro ao gerar toString() da classe "
								+ obj.getClass().getName());
				stringbuffer.append("\nErro: ").append(exception1);
			} finally {
				stringbuffer.append("\n\n============= [ ").append(s)
						.append(" - FIM ] =============\n\n");
			}
		}
		return stringbuffer.toString();
	}

	public static List getFields(Class class1) {
		ArrayList arraylist = new ArrayList();
		if (class1.getDeclaredFields() != null) {
			arraylist.addAll(Arrays.asList(class1.getDeclaredFields()));
			if (class1.getSuperclass() != null)
				arraylist.addAll(getFields(class1.getSuperclass()));
		}
		return arraylist;
	}

	public static Object getValue(Object obj, String s) throws Exception {
		return getValue(obj, s, new Class[0], new Object[0]);
	}

	public static Object getValue(Object obj, String s, Class aclass[],
			Object aobj[]) throws Exception {
		Method method = obj.getClass().getMethod(
				"get" + s.substring(0, 1).toUpperCase() + s.substring(1),
				aclass);
		return method.invoke(obj, aobj);
	}
}
